package com.black.knight.chess.model;

import com.black.knight.chess.R;
import com.black.knight.chess.StartPositionActivity;
import com.black.knight.chess.utils.ChessUtil;

public class SPModel {
	
	private static SPModel model;
	public static StartPositionActivity context;
	
	private boolean whitePlayFirst = true;

	public static SPModel getInstance() {
		if(model == null) {
			model = new SPModel();
		}
		return model;
	}
	
	private Integer[] fields = {
            R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.bk, R.drawable.empty, R.drawable.empty, R.drawable.empty,
            R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty,
            R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty,
            R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty,
            R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty,
            R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty,
            R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty,
            R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.empty, R.drawable.wk, R.drawable.empty, R.drawable.empty, R.drawable.empty,
    };
	
	public Integer[] getFields() {
		return fields;
	}

	private Integer[] destroyedFiguresBlack = {
			R.drawable.br, R.drawable.bn, R.drawable.bb, R.drawable.bq, R.drawable.empty, R.drawable.bb, R.drawable.bn, R.drawable.br,
            R.drawable.bp, R.drawable.bp, R.drawable.bp, R.drawable.bp, R.drawable.bp, R.drawable.bp, R.drawable.bp, R.drawable.bp,
	};
	
	public Integer[] getDestroyedFiguresBlack() {
		return destroyedFiguresBlack;
	}

	private Integer[] destroyedFiguresWhite = {
            R.drawable.wr, R.drawable.wn, R.drawable.wb, R.drawable.wq, R.drawable.empty, R.drawable.wb, R.drawable.wn, R.drawable.wr,
            R.drawable.wp, R.drawable.wp, R.drawable.wp, R.drawable.wp, R.drawable.wp, R.drawable.wp, R.drawable.wp, R.drawable.wp,
	};
	
	public Integer[] getDestroyedFiguresWhite() {
		return destroyedFiguresWhite;
	}

	private Integer selectedFlagBlack = -1;
	private Integer selectedFlagWhite = -1;
	
	private Integer selectedFlagTable = -1;
	
	private Integer selectedFigure;

	public SPModel() {
		reset();
	}
	
	public void reset() {
	}

	public Integer getSelectedFlagBlack() {
		return selectedFlagBlack;
	}

	public void setSelectedFlagBlack(Integer selectedFlagBlack) {
		this.selectedFlagWhite = -1;
		this.selectedFlagTable = -1;
		this.selectedFlagBlack = selectedFlagBlack;
		
		if(selectedFlagBlack != -1) selectedFigure = destroyedFiguresBlack[selectedFlagBlack];
	}

	public Integer getSelectedFlagWhite() {
		return selectedFlagWhite;
	}

	public void setSelectedFlagWhite(Integer selectedFlagWhite) {
		this.selectedFlagBlack = -1;
		this.selectedFlagTable = -1;
		this.selectedFlagWhite = selectedFlagWhite;
		
		if(selectedFlagWhite != -1) selectedFigure = destroyedFiguresWhite[selectedFlagWhite];
	}

	public Integer getSelectedFlagTable() {
		return selectedFlagTable;
	}

	public void setSelectedFlagTable(Integer selectedFlagTable) {
		this.selectedFlagTable = selectedFlagTable;
		this.selectedFlagBlack = -1;
		this.selectedFlagWhite = -1;
		
		if(selectedFlagTable != -1) selectedFigure = fields[selectedFlagTable];
	}
	
	public Integer getSelectedFigure() {
		return selectedFigure;
	}
	
	public void setSelectedFigure(Integer selectedFigure) {
		this.selectedFigure = selectedFigure;
	}
	
	public void destroyFigure(Integer figure, boolean white) {
		Integer[] destroyedFigures = destroyedFiguresWhite;
		
		if(!white) {
			destroyedFigures = destroyedFiguresBlack;
		}

		for(int i = 0; i<destroyedFigures.length; i++) {
			if(destroyedFigures[i] == R.drawable.empty) {
				destroyedFigures[i] = figure;
				break;
			}
		}
	}
	
	public String getFEN() {
		StringBuffer fenBuffer = new StringBuffer();
		
		int counter = 0;
		for(int i = 0; i<=7; i++) {
			for(int j = 0; j<=7; j++) {
				int figure = getFigure(i, j);
				String flag = ChessUtil.getFigureFANFlag(figure);
				
				if(flag.length() == 0) {
					counter++;
				}
				
				if(j == 7) {
					if(flag.length() == 0) {
						fenBuffer.append(counter);
					} else {
						if(counter > 0) fenBuffer.append(counter);
						fenBuffer.append(flag);
					}
					
					fenBuffer.append("/");
					
					counter = 0;
					continue;
				}
				
				if(flag.length() == 0) {
					continue;
				}
				
				if(counter > 0) {
					fenBuffer.append(counter);
					counter = 0;
				}
				
				fenBuffer.append(flag);
			}
		}
		
		fenBuffer.delete(fenBuffer.length() - 1, fenBuffer.length());
		
		String fenPlayer = whitePlayFirst ? "w" : "b";
		
		String fenCastleWK = "";
		String fenCastleWQ = "";
		String fenCastleBK = "";
		String fenCastleBQ = "";
		
		String halfMove = "0";
		String fullMove = "1";
		
		fenCastleWK = "K";
		fenCastleWQ = "Q";
		fenCastleBK = "k";
		fenCastleBQ = "q";
		
		String fenCastle = fenCastleWK + fenCastleWQ + fenCastleBK + fenCastleBQ;
		
		if(fenCastle.length() == 0) fenCastle = "-";
		
		String fenEnPassant = "-";
		
		fenBuffer.append(" ").append(fenPlayer).append(" ").append(fenCastle).append(" ").append(fenEnPassant).append(" ").append(halfMove).append(" ").append(fullMove);
		
		return fenBuffer.toString();
	}
	
	public int getFigure(int row, int column) {
		if(row*8 + column > 63 || row*8 + column < 0) return R.drawable.empty;
		
		return fields[row*8 + column];
	}
	
	public boolean isWhitePlayFirst() {
		return whitePlayFirst;
	}

	public void setWhitePlayFirst(boolean whitePlayFirst) {
		this.whitePlayFirst = whitePlayFirst;
	}
}
